package com.test.realms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.test.base.entity.Perms;
import com.test.base.entity.User;
import com.test.base.service.UserService;
import com.test.jwt.JWTToken;
import com.test.redis.CustomSimpleByteSource;
import com.test.shiro.MyByteSource;
import com.test.utils.ApplicationContextUtil;
import com.test.utils.JWTUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author ssang
 * @date 2021-12-09 10:55
 */
public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserService userServiceImpl;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("--------------------开始授权--------------------");
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
        UserService userServiceImpl = (UserService) ApplicationContextUtil.getBean("userServiceImpl");
        //获取用户
        List<User> userList = userServiceImpl.getUserList(primaryPrincipal);
        if(userList==null || userList.size()==0){
            return null;
        }
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        User user = userList.get(0);
        //遍历用户角色
        user.getRoleList().forEach(role -> {
            //获取角色对应的资源权限
            List<Perms> permsList = userServiceImpl.getPermByRole(role.getId());
            permsList.forEach(perms -> {
                //添加资源权限
                simpleAuthorizationInfo.addStringPermission(perms.getName());
            });
            //添加角色权限
            simpleAuthorizationInfo.addRole(role.getRolename());
        });

        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("--------------------开始认证--------------------");
        String principal = (String) authenticationToken.getPrincipal();
        if(!StringUtils.isNullOrEmpty(principal)){
//            UserService userServiceImpl = (UserService) ApplicationContextUtil.getBean("userServiceImpl");
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("username",principal);
            //查询用户
            User user = userServiceImpl.getOne(wrapper);
            //如果存在，返回
            if(user!=null){
//                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),
//                        ByteSource.Util.bytes(user.getSalt()),this.getName());
//                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),
//                        new CustomSimpleByteSource(user.getSalt()),this.getName());
                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),
                        new MyByteSource(user.getSalt()),this.getName());
                return info;
            }
        }
        return null;
    }

//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        System.out.println("--------------------开始认证--------------------");
//        JWTToken jwtToken = (JWTToken) token;
//        String tokenStr = (String) jwtToken.getPrincipal();
//        String userName = JWTUtil.getUsername(tokenStr);
//
//        if(userName==null){
//            throw new AuthenticationException();
//        }
//
//
//
//        return null;
//    }
}
